from django.contrib import messages
from django.contrib import admin
from django.contrib.auth.models import User,Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin,GroupAdmin as BaseGroupAdmin
from django.utils.translation import gettext_lazy as _
from import_export.admin import ImportExportActionModelAdmin

from .models import *
from .flylu.flyluAM import *

from unfold.admin import ModelAdmin,UnfoldAdminSelectWidget,UnfoldAdminTextInputWidget
from unfold.forms import AdminPasswordChangeForm,UserChangeForm,UserCreationForm
from unfold.contrib.import_export.forms import ExportForm,ImportForm,SelectableFieldsExportForm

# django_celery_beat
from django_celery_beat.models import (
    ClockedSchedule,
    CrontabSchedule,
    IntervalSchedule,
    PeriodicTask,
    SolarSchedule,
)
from django_celery_beat.admin import ClockedScheduleAdmin as BaseClockedScheduleAdmin
from django_celery_beat.admin import CrontabScheduleAdmin as BaseCrontabScheduleAdmin
from django_celery_beat.admin import PeriodicTaskAdmin as BasePeriodicTaskAdmin
from django_celery_beat.admin import PeriodicTaskForm, TaskSelectWidget

# django_celery_results
from django_celery_results.models import(
    GroupResult,
    TaskResult
)

from django_celery_results.admin import TaskResultAdmin as BaseTaskResultAdmin
from django_celery_results.admin import GroupResultAdmin as BaseGroupResultAdmin

# django_celery_beat
admin.site.unregister(PeriodicTask)
admin.site.unregister(IntervalSchedule)
admin.site.unregister(CrontabSchedule)
admin.site.unregister(SolarSchedule)
admin.site.unregister(ClockedSchedule)

# django_celery_results
admin.site.unregister(GroupResult)
admin.site.unregister(TaskResult)


flylu =  FlyLu()

admin.site.unregister(User)
admin.site.unregister(Group)

@admin.register(User)
class UserAdmin(BaseUserAdmin,ModelAdmin):
    form = UserChangeForm
    add_form = UserCreationForm
    change_password_form = AdminPasswordChangeForm

@admin.register(Group)
class GroupAdmin(BaseGroupAdmin, ModelAdmin):
    pass

# 文章发布按钮
def relesses_article(modeladmin, request, queryset):
    for obj in queryset:
        bookName = getattr(obj, 'bookName')
        relessesUrl = getattr(obj, 'relessesUrl')
        proxyHost = getattr(obj,'proxyHost')
        proxyPort = getattr(obj,'proxyPort')
        proxyUser = getattr(obj,'proxyUser')
        proxyPass = getattr(obj,'proxyPass')
        pluginPath = getattr(obj,'pluginPath')
        wordPath = getattr(obj, 'wordPath')
        startPar = getattr(obj, 'startPar')
        endPar = getattr(obj, 'endPar')
        selectFlag = getattr(obj, 'selectFlag')
        # 获取外键关联的实例
        related_obj = obj.userName
        userName = getattr(related_obj, 'userName')
        passWord = getattr(related_obj, 'passWord')
        print({'bookName':bookName,'relessesUrl':relessesUrl,'wordPath':wordPath,
               'startPar':startPar,'endPar':endPar,'selectFlag':selectFlag,
               'userName':userName,'passWord':passWord})
        flylu.add_article(relessesUrl,proxyHost,proxyPort,proxyUser,proxyPass,pluginPath,userName,passWord,wordPath,startPar,endPar,selectFlag)
        messages.success(request, _('方法已执行，页面加载完成。'))
relesses_article.short_description = '发布'
relesses_article.type = 'info'

@admin.register(FlyUser)
class FlyUserAdmin(ModelAdmin,ImportExportActionModelAdmin):
    list_display = ['userName', 'addTime']
    import_form_class=ImportForm
    export_form_class=ExportForm
    list_per_page = 20
    
@admin.register(FlyConfig)
class FlyConfigAdmin(ModelAdmin):
    actions = [relesses_article]
    list_display = ('userName', 'bookName','currentChapter','startPar','endPar')
    list_per_page = 20
    list_filter=['userName','bookName']

# django_celery_beat
class UnfoldTaskSelectWidget(UnfoldAdminSelectWidget, TaskSelectWidget):
    pass


class UnfoldPeriodicTaskForm(PeriodicTaskForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["task"].widget = UnfoldAdminTextInputWidget()
        self.fields["regtask"].widget = UnfoldTaskSelectWidget()


@admin.register(PeriodicTask)
class PeriodicTaskAdmin(BasePeriodicTaskAdmin, ModelAdmin):
    form = UnfoldPeriodicTaskForm


@admin.register(IntervalSchedule)
class IntervalScheduleAdmin(ModelAdmin):
    pass


@admin.register(CrontabSchedule)
class CrontabScheduleAdmin(BaseCrontabScheduleAdmin, ModelAdmin):
    pass


@admin.register(SolarSchedule)
class SolarScheduleAdmin(ModelAdmin):
    pass

@admin.register(ClockedSchedule)
class ClockedScheduleAdmin(BaseClockedScheduleAdmin, ModelAdmin):
    pass

# django_celery_results
@admin.register(GroupResult)
class GroupResultAdmin(BaseGroupResultAdmin, ModelAdmin):
    pass
@admin.register(TaskResult)
class TaskResultAdmin(BaseTaskResultAdmin, ModelAdmin):
    pass